package com.honour.busserver.backup.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.honour.busserver.backup.base.ResultMsg;
import com.honour.busserver.backup.config.WebSocket;
import com.honour.busserver.backup.entity.BusBackConfig;
import com.honour.busserver.backup.entity.BusBackFile;
import com.honour.busserver.backup.entity.CentOSOneEntity;
import com.honour.busserver.backup.entity.SSHRegisterEntity;
import com.honour.busserver.backup.mapper.BusBackConfigMapper;
import com.honour.busserver.backup.mapper.BusBackFileMapper;
import com.honour.busserver.backup.service.BusBackFileService;
import com.honour.busserver.backup.utils.SshCommanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class BusBackFileServiceImpl extends ServiceImpl<BusBackFileMapper, BusBackFile> implements BusBackFileService {
    @Autowired
    private CentOSOneEntity centOSOneEntity;
    @Autowired
    private BusBackConfigMapper busBackConfigMapper;
    @Autowired
    private WebSocket webSocket;
    @Value("${centos.dir}")
    private String dir;

    public ResultMsg exec(String db) throws IOException {
        String[] split = db.split(",");
        QueryWrapper<BusBackConfig> wrapper = new QueryWrapper<>();
        List<BusBackConfig> busBackConfigs = busBackConfigMapper.selectList(wrapper);

        if (busBackConfigs != null && busBackConfigs.size() > 0) {
            dir = busBackConfigs.get(0).getLocation();
        }
        for (int i = 0; i < split.length; i++) {
            // TODO 需要改为按照模式备份
            SSHRegisterEntity sshRegisterEntity = new SSHRegisterEntity(centOSOneEntity);
            StringBuilder exec = sshRegisterEntity.exec(SshCommanUtil.dmBackUpcommands(centOSOneEntity.getDbUser(), centOSOneEntity.getDbPassword(), dir, centOSOneEntity.getBin()));
            //任务执行返回值
            String res = exec.toString();
        }
//        webSocket.sendAllMessage("50");
        return new ResultMsg(true);
    }

    @Override
    public ResultMsg deleteFile(String id) {
        try {
            //删除服务器数据
            //删除文件信息
            QueryWrapper<BusBackFile> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            baseMapper.delete(queryWrapper);
            return new ResultMsg(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg(false);
        }


    }

    @Override
    public ResultMsg getFileExist(String id) {
        QueryWrapper<BusBackFile> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        boolean exists = baseMapper.exists(wrapper);
        return new ResultMsg(true,200,exists);
    }

    @Override
    public List<BusBackFile> fileList() {
        QueryWrapper<BusBackFile> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("time");
        return baseMapper.selectList(wrapper);
    }

}
